package com.trg.designmodel.singleleton;

/**
 * @ClassName: SingleLetonTest05
 * @Description:    单例设计模式-懒汉式（线程安全-同步代码块）
 * @Author: tangrg
 * @Date: 2021/6/30 16:03
 * @Version: 1.0
 * @Return:
 **/
public class SingleLetonTest05 {
    public static void main(String[] args) {
        SingleLeton05 instance = SingleLeton05.getInstance();
        SingleLeton05 instance2 = SingleLeton05.getInstance();
        System.out.println("单例设计模式-懒汉式（线程安全-同步代码块）");
        System.out.println(instance == instance2);
        System.out.println("instance:" + instance.hashCode());
        System.out.println("instance2:" + instance2.hashCode());
    }
}

class SingleLeton05 {
    private static SingleLeton05 instance;


    private SingleLeton05() {
    }

    public static SingleLeton05 getInstance() {
        if (instance == null) {
            synchronized (SingleLeton05.class) {
                instance = new SingleLeton05();
            }
        }
        return instance;
    }


}